﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain
{
	using System.Collections.Generic;
	using Falcon.Core.Extensions;

	public partial class Resources
	{
		private static readonly Dictionary<string, string> Values = new Dictionary<string, string>();

		/// <summary>
		/// Initializes the specified resources.
		/// </summary>
		/// <param name="resources">The resources.</param>
		public void Init(IList<Resource> resources)
		{
			lock (Values)
			{
				if (Values.Count > 0)
				{
					return;
				}

				foreach (var resource in resources)
				{
					Values.Add(resource.Key, resource.Value);
				}
			}
		}

		/// <summary>
		/// Rebuilds the specified resources.
		/// </summary>
		/// <param name="resources">The resources.</param>
		public void Rebuild(IList<Resource> resources)
		{
			lock (Values)
			{
				Values.Clear();

				foreach (var resource in resources)
				{
					Values.Add(resource.Key, resource.Value);
				}
			}
		}

		/// <summary>
		/// Determines whether resource exists using the specified key.
		/// </summary>
		/// <param name="key">The key.</param>
		/// <returns>
		/// 	<c>true</c> if resource exists; otherwise, <c>false</c>.
		/// </returns>
		public static bool Contains(string key)
		{
			return Values.ContainsKey(key);
		}

		/// <summary>
		/// Gets the resource by key.
		/// </summary>
		/// <param name="key">The key.</param>
		/// <returns></returns>
		public static string GetByKey(string key)
		{
			return Values.ContainsKey(key) ? Values[key] : key;
		}

		/// <summary>
		/// Gets the resource by key formatted with the specified parameters.
		/// </summary>
		/// <param name="key">The key.</param>
		/// <param name="parameters">The parameters.</param>
		/// <returns></returns>
		public static string GetByKey(string key, params object[] parameters)
		{
			return GetByKey(key).Fmt(parameters);
		}
	}
}